#pragma once

#include "comm/types.h"
#include "tools/bitmap.h"
#include "ipc/mutex.h"
#include "comm/boot_info.h"

//1MB以下为内核空间
#define MEM_EXT_START   (1024*1024)
//页大小
#define PAGE_SIZE       4096
//操作系统内存底线
#define MEM_EBDA_START  0x00080000

#define MEM_TASK_BASE   0x80000000

#define MEM_STACK_TOP   0xE0000000
#define MEM_STACK_SIZE  (PAGE_SIZE*500)

#define MEM_TASK_ARG_SIZE (PAGE_SIZE * 4)


//qemu内存128MB
#define MEM_EXT_END     (128*1024*1024 - 1)

//内存分配控制
typedef struct addr_alloc_t
{
    mutex_t mutex;
    bitmap_t bitmap;
    //起始地址
    uint32_t start;
    //大小
    uint32_t size;
    //页大小
    uint32_t page_size;

}addr_alloc_t;

typedef struct _memory_map_t
{
    void* vstart;
    void* vend;
    void* pstart;
    uint32_t perm;
}memory_map_t;



void mermory_init(boot_info_t* boot_info);

//创建用户虚拟内存空间
uint32_t memory_create_uvm();

//为虚拟内存分配物理地址
int memory_alloc_page_for(uint32_t addr,uint32_t size,int perm);
//分配一页内存
uint32_t memory_alloc_page();
//释放一页内存
void memory_free_page(uint32_t addr);

//释放页表
void  memory_destroy_uvm(uint32_t page_dir);
//创建页表
uint32_t memory_copy_uvm(uint32_t page_dir);

int memory_alloc_for_page_dir(uint32_t page_dir,uint32_t vaddr,uint32_t size,int perm);

uint32_t memory_get_paddr(uint32_t page_dir, uint32_t vaddr);

int memory_copy_uvm_data(uint32_t to, uint32_t page_dir, uint32_t from, uint32_t size) ;

char * sys_sbrk(int incr);